﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using HDVN5.srvDangNhap;
namespace HDVN5.Account
{
    public partial class Login : Page
    {
        private DangNhapTaiKhoanSoapClient client = new DangNhapTaiKhoanSoapClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            string userName = UserName.Text;
            string passWord = Password.Text;
            string logined = client.dangNhapTaiKhoan(UserName.Text, Password.Text);

            if (logined == "A")
            {
                LoginModule.AccountModuleDataContext accDb = new LoginModule.AccountModuleDataContext();
                FormsAuthentication.SetAuthCookie(UserName.Text, true);
                LoginModule.DANGKYSUDUNG dky = accDb.DANGKYSUDUNGs.SingleOrDefault(p => p.TENTAIKHOAN == UserName.Text.Trim().ToUpper());
                int capDangNhap = (int)dky.CAPDANGKY;
                Session.Add("LOGIN_ID", UserName.Text);
                Session.Add("MADONVIID", dky.MASO);
                Session.Add("CAP", dky.CAPDANGKY);

                FormsAuthenticationTicket tkt;
                string cookiestr;
                HttpCookie ck;
                tkt = new FormsAuthenticationTicket(1, UserName.Text, DateTime.Now,
                DateTime.Now.AddMinutes(30), false, "your custom data");
                cookiestr = FormsAuthentication.Encrypt(tkt);
                ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
                ck.Path = FormsAuthentication.FormsCookiePath;

                Response.Cookies.Add(ck);

                if (capDangNhap == 1 || capDangNhap == 2 || capDangNhap == 4)
                {
                    Response.Redirect("/DaoLienDoan/CapTrucThuocBDH.aspx");
                }
                else
                {
                    Response.Redirect("/DonViNganhPage/DonViNganhHomePage.aspx");
                }
                   
            }
        }
    }
}